﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AutoFacUtils;
using FluentValidation;
using CashFlow.Web.Areas.SystemManage.Models.Shop;
using IUserService3 = CashFlow.Web.Areas.SystemManage.Service.IUserService;
using CashFlow.Web.Common;

namespace CashFlow.Web.Areas.SystemManage.Validator.Shop
{
    public class NewShopModelValidator
        : AbstractValidator<NewShopModel>
    {
        private IUserService3 GetUserService3() { return AutoFacHelper.Get<IUserService3>(); }

        public NewShopModelValidator()
        {
            CascadeMode = CascadeMode.Stop;
            RuleFor(x => x.ShopManagerUserId).Must(x => IsUserExist(x))
                .WithMessage("非法ShopManagerUserId");
            RuleFor(x => x.ShopName).Must(x => RegExpTool.IsName(x))
                .WithMessage("非法ShopName");
            RuleFor(x => x.ShopNo).Must(x => RegExpTool.IsShopNo(x))
                .WithMessage("非法ShopNo");
        }

        private bool IsUserExist(int userId)
        {
            var service3 = GetUserService3();
            var count = service3.Count(x => x.Id == userId);
            return count > 0;
        }
    }
}